package me.green.ui.dialog;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.sql.Timestamp;
import java.util.Date;
import java.util.logging.Logger;

import javax.swing.*;

import me.green.bean.Diary;
import me.green.dao.DiaryDao;
import me.green.ui.LocalCache;
import me.green.ui.global.UIResources;
import me.green.ui.global.UIStyle;
import me.green.ui.global.i18n.UIText;
import me.green.util.DateUtil;
import me.green.ui.event.ShortcutKeyListener;
import com.toedter.calendar.JDateChooser;

/**
 * 日记窗口
 *
 * @author green
 */
public class DiaryDialog extends JDialog {
    private Logger logger = Logger.getLogger(DiaryDialog.class.getName());
    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JLabel labMood = null;
    private JLabel labDate = null;
    private JLabel labSubject = null;
    private JLabel labDetail = null;

    private JDateChooser txtDate = null;
    private JTextField txtSubject = null;
    private JTextArea txtDetail = null;

    private JScrollPane jScrollPane = null;

    private JButton btnAdd = null;
    private JButton btnClear = null;

    private Diary diary;
    private boolean isUpdateDialog = false;

    private String[][] btnStatusAttributes = {
            {"美滋滋", "元气满满", "emo", "疲惫", "发呆", "其他"},
            {"MZZ", "YQMM", "EMO", "PB", "FD", "QT"}
    };

    private ButtonGroup buttonGroup;

    public DiaryDialog(JFrame owner, boolean modal) {
        super(owner, modal);
        initialize();
        this.setLocationRelativeTo(owner);
        this.setVisible(true);
    }

    public DiaryDialog(JFrame owner, boolean modal, Diary diary) {
        this(owner, modal);
        this.diary = diary;
        this.isUpdateDialog = true;
        this.fillValue();
    }

    /**
     * 显示已有的日记值
     */
    private void fillValue() {
        if (null != diary) {

        }
    }

    /**
     * This method initializes txtDate
     *
     * @return javax.swing.JTextField
     */
    private JDateChooser getTxtDate() {
        if (txtDate == null) {
            txtDate = new JDateChooser();
            txtDate.setBounds(new Rectangle(85, 60, 214, 30));
            txtDate.setDate(new Date());
        }
        return txtDate;
    }

    /**
     * 初始化标题输入框
     *
     * @return javax.swing.JTextField
     */
    private JTextField getTxtSubject() {
        if (txtSubject == null) {
            txtSubject = new JTextField();
            txtSubject.setFont(UIStyle.defaultFont());
            txtSubject.setMargin(new Insets(0,5,0,5));
            txtSubject.setBounds(new Rectangle(85, 110, 450, UIStyle.INPUT_HEIGHT));
            txtSubject.addKeyListener(new java.awt.event.KeyAdapter() {
                @Override
                public void keyPressed(java.awt.event.KeyEvent e) {
                    logger.info("text subject press key code: " + e.getKeyCode());
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        txtDetail.requestFocus();
                    } else {
                        return;
                    }
                }
            });
        }
        return txtSubject;
    }

    /**
     * This method initializes jScrollPane
     *
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setBounds(new Rectangle(85, 160, 660, 310));
            jScrollPane.setViewportView(getTxtDetail());
        }
        return jScrollPane;
    }

    /**
     * This method initializes btnAdd
     *
     * @return javax.swing.JButton
     */
    private JButton getBtnAdd() {
        if (btnAdd == null) {
            btnAdd = new JButton();
            btnAdd.setBounds(new Rectangle(85, 480, 70, 36));
            btnAdd.setFont(UIStyle.defaultFont());
            btnAdd.setText(UIStyle.text(UIText.SAVE));
            btnAdd.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    saveDiary();
                }
            });
        }
        return btnAdd;
    }

    /**
     * This method initializes btnClear
     *
     * @return javax.swing.JButton
     */
    private JButton getBtnClear() {
        if (btnClear == null) {
            btnClear = new JButton();
            btnClear.setBounds(new Rectangle(180, 480, 70, 36));
            btnClear.setText(UIStyle.text(UIText.CLOSE));
            btnClear.setFont(UIStyle.defaultFont());
            btnClear.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    DiaryDialog.this.setVisible(false);
                    DiaryDialog.this.dispose();
                }
            });
        }
        return btnClear;
    }

    /**
     * This method initializes txtDetail
     *
     * @return javax.swing.JTextArea
     */
    private JTextArea getTxtDetail() {
        if (txtDetail == null) {
            txtDetail = new JTextArea();
            txtDetail.setFont(UIStyle.defaultFont());
            txtDetail.setWrapStyleWord(true);
            // 自动换行
            txtDetail.setLineWrap(true);
            txtDetail.setMargin(new Insets(5, 5, 5, 5));
        }
        return txtDetail;
    }


    /**
     * 初始化
     *
     * @return void
     */
    private void initialize() {
        this.setSize(800, 600);
        this.setTitle(UIStyle.text(UIText.TITLE_ADD_DIARY));
        this.setResizable(false);
        this.setContentPane(getJContentPane());
        this.setIconImage(new ImageIcon(this.getClass().getResource(UIResources.ICON_TRAY)).getImage());
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        Toolkit.getDefaultToolkit().addAWTEventListener(ShortcutKeyListener.getInstance(this),
                AWTEvent.KEY_EVENT_MASK);
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);

            labMood = new JLabel();
            labMood.setBounds(new Rectangle(10, 10, 100, UIStyle.INPUT_HEIGHT));
            labMood.setText(UIStyle.text(UIText.LABEL_DIARY_MOOD));
            labMood.setFont(UIStyle.defaultFont());

            JPanel panelRadioButtons = new JPanel(new FlowLayout(FlowLayout.LEFT));
            buttonGroup = new ButtonGroup();
            for (int i = 0; i < btnStatusAttributes[0].length; i++) {
                String text = btnStatusAttributes[0][i];
                JRadioButton button = new JRadioButton(text, false);
                button.setActionCommand(btnStatusAttributes[1][i]);
                button.setFont(UIStyle.defaultFont());
                panelRadioButtons.add(button);
                if (i == 0) {
                    button.setSelected(true);
                }
                buttonGroup.add(button);
            }
            panelRadioButtons.setBounds(new Rectangle(85, 10, 650, UIStyle.INPUT_HEIGHT));
            jContentPane.add(panelRadioButtons);


            labDate = new JLabel();
            labDate.setFont(UIStyle.defaultFont());
            labDate.setBounds(new Rectangle(10, 60, UIStyle.INPUT_WIDTH, UIStyle.INPUT_HEIGHT));
            labDate.setText(UIStyle.text(UIText.LABEL_DIARY_DATE));

            labSubject = new JLabel();
            labSubject.setFont(UIStyle.defaultFont());
            labSubject.setBounds(new Rectangle(10, 110, UIStyle.INPUT_WIDTH, UIStyle.INPUT_HEIGHT));
            labSubject.setText(UIStyle.text(UIText.LABEL_DIARY_SUBJECT));

            labDetail = new JLabel();
            labDetail.setFont(UIStyle.defaultFont());
            labDetail.setBounds(new Rectangle(10, 160, UIStyle.INPUT_WIDTH, 100));
            labDetail.setText(UIStyle.text(UIText.LABEL_DIARY_CONTENT));

            jContentPane.add(labMood);
            jContentPane.add(labDate);
            jContentPane.add(getTxtDate());

            jContentPane.add(labSubject, null);
            jContentPane.add(getTxtSubject(), null);
            jContentPane.add(labDetail, null);
            jContentPane.add(getJScrollPane(), null);
            jContentPane.add(getBtnAdd(), null);
            jContentPane.add(getBtnClear(), null);
        }
        return jContentPane;
    }

    /**
     * 添加[完成工作]日志
     */
    public void saveDiary() {
        String company = buttonGroup.getSelection().getActionCommand();
        String subject = txtSubject.getText().trim();
        String detail = txtDetail.getText().trim();
        java.util.Date occurDate = txtDate.getDate();

        if (subject.equals("")) {
            JOptionPane.showMessageDialog(this, "请输入主题！",
                    UIStyle.text(UIText.TIP), 0);
            txtSubject.requestFocus();
            return;
        }
        if (occurDate == null) {
            JOptionPane.showMessageDialog(this, "日期格式错误了哦！",
                    UIStyle.text(UIText.TIP), 0);
            txtDate.cleanup();
            txtDate.requestFocus();
            return;
        }
        if (occurDate.after(new Date())) {
            JOptionPane.showMessageDialog(this, "选择今天之前的日期",
                    UIStyle.text(UIText.TIP), 0);
            return;
        }

        Timestamp add_date = new Timestamp(occurDate.getTime());
        Diary cd = new Diary();
        cd.setMood(company);
        cd.setAddDate(add_date);
        cd.setAbstractWork(subject);
        cd.setDetail(detail);
        cd.setUid(LocalCache.getUser().getId());

        boolean flag = new DiaryDao().saveDiary(cd);
        if (flag) {
            JOptionPane.showMessageDialog(DiaryDialog.this, UIStyle.text(UIText.SUCCESS_SAVE));
            DiaryDialog.this.setVisible(false);
            java.sql.Date weekBefore = (java.sql.Date) DateUtil.getWeekBegin(0);
            java.sql.Date weekEnd = (java.sql.Date) DateUtil.getWeekEnd(weekBefore);
            LocalCache.getFrameApp().updateEditorPaneContent();
            DiaryDialog.this.dispose();
        } else {
            JOptionPane.showMessageDialog(DiaryDialog.this, UIStyle.text(UIText.ERROR_SAVE),
                    UIStyle.text(UIText.TIP), 0);
        }
    }
}
